<!--
 * @Author: GongSaiPeng
 * @Date: 2024-04-07 19:17:35
 * @LastEditTime: 2024-04-07 19:41:27
 * @LastEditors: GongSaiPeng
 * @Description: 利用elementui的upload组件和Progress 进度条实现上传显示进度条功能
 * @FilePath: \main-app\src\components\uploadEl\index.vue
-->
<script>
//导入axios
import axios from "axios";
export default {
  name: "UploadEl",
  data() {
    return {
      // 进度条
      fileList: [],
      showFailTip: false,
      customColors: [
        { color: "rgba(223,228,237,0.10)", percentage: 0 },
        { color: "#00adc9", percentage: 100 },
      ],
      fileElProgress: false,
      fileProgressText: "",
      progressPercent: 0,
    };
  },
  methodss: {
    uploadVersion(params) {
      console.log(params);
      const _this = this;
      this.uploadForm.filename = params.file.name;
      this.fileFormData = new FormData();
      this.fileFormData.append("file", params.file);
      this.fileFormData.append("md5File", params.file);
      this.fileName = params.file.name;
      const config = {
        onUploadProgress: (progressEvent) => {
          if (progressEvent.lengthComputable) {
            _this.uploadProgress = Math.round(
              (progressEvent.loaded / progressEvent.total) * 100
            );
            console.log("progressEvent>>", progressEvent);
            console.log("uploadProgress>>", _this.uploadProgress);
            this.fileElProgress = true;
            if (this.progressPercent < 99) {
              this.progressPercent = _this.uploadProgress;
            } else {
              this.fileProgressText = "正在上传";
            }
          }
        },
      };
      console.log(this.fileFormData, config);
      axios
        .get("https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15", {
          params: {
            name: "wendy",
            age: 18,
          },
        })
        .then((res) => {
          console.log(res);
          if (res.data === "success") {
            this.fileProgressText = "上传成功";
          } else {
            this.showFailTip = true;
          }
          this.fileElProgress = false;
        });
    },
  },
};
</script>

<template>
  <div>
    <el-upload
      v-show="!fileList.length"
      ref="fileUpload"
      class="upload-demo"
      style="display: inline-block; height: 32px; margin-left: 8px"
      action="#"
      :file-list="fileList"
      :http-request="uploadVersion"
      :on-change="handleChange"
      :on-success="handleSuccess"
      :on-progress="handleProgress"
      :on-error="handleError"
      :auto-upload="true"
      :show-file-list="false"
    >
      <el-button
        type="primary"
        style="height: 32px; display: flex; align-items: center"
      >
        <svg-icon icon-class="icon_upload" style="margin-right: 8px" />
        上传文件
      </el-button>
    </el-upload>
    <div v-if="fileElProgress">
      <div class="el-progress-div">
        <div>
          <div
            v-loading="true"
            style="
              display: inline-block;
              width: 24px;
              height: 16px;
              padding-right: 8px;
            "
          >
            {{ fileName }}
          </div>
          <span>
            <span style="display: inline-block; margin-right: 8px"
              >{{ progressPercent }}%</span
            >
            <el-button type="text" @click="cancelUpload">取消</el-button>
          </span>
        </div>
        <el-progress
          :percentage="progressPercent"
          :text-inside="false"
          :color="customColors"
          :stroke-width="2"
        />
      </div>
      <template v-if="!fileElProgress">
        <div v-for="item in fileList" :key="item.name" class="fail-list">
          <div class="list-item">
            <span :style="{ color: showFailTip ? '#FF3D00' : '#fff' }">
              <svg-icon
                :icon-class="
                  showFailTip ? 'icon_error_file' : 'icon_success_file'
                "
              />
              {{ item.name }}
            </span>
            <span style="float: right; display: flex; align-items: center">
              <span
                style="
                  color: #878d99;
                  display: inline-block;
                  margin-right: 16px;
                "
                >{{ showFailTip ? "失败" : "成功" }}</span
              >
              <el-button
                style="color: #00e3ff"
                type="text"
                size="small"
                @click="fileList = []"
                >删除</el-button
              >
              <el-button
                v-show="showFailTip"
                style="color: #00e3ff"
                type="text"
                size="small"
                @click="sendClick"
                >重新上传</el-button
              >
            </span>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<style lang="less" scoped></style>
